const a = function ({ transfer: { type = "" } }, { $grid, xe: { get } }) {
  $grid.getColumns().map(({ params, field }) => {
    const isDynamic = field && get(params, "ui.custom.dynamic", false);
    if (isDynamic) $grid.hideColumn($grid.getColumnByField(field));
  });
  if (type == "yszx")
    ["MONEY2", "MONEY3", "MONEY4", "ZXL"].map(function (col) {
      $grid.showColumn($grid.getColumnByField(col));
    });
};

const b = async function (
  params,
  { transfer, dataId, memory, origin },
  { $grid, dc: { http }, xe: { clone, get, omit } }
) {
  console.log("load grid data: ", $grid, params, dataId);
  const post = omit(params, ["guid"]);
  if (params.guid && params.guid.join("") != "0")
    post.enterpriseGuid = params.guid.join("");
  const form = clone(get(origin, "1797803362328653826", {}), true);
  const data = { ...post, ...omit(form ?? {}, ["condition", "dataCondition"]) };
  const conditionList = get(memory, "condition", []);
  const conditions = form.condition
    ? conditionList
        .filter(function ({ id }) {
          return form.condition.indexOf(id) > -1;
        })
        .map(function ({ condition }) {
          return condition;
        })
    : [];
  if (conditions.length) {
    data["condition"] = conditions.join("");
  }
  conditionList.map(({ condition }) => {
    const fields = condition.slice(0, -1).split(",");
    if (conditions.indexOf(condition) > -1) {
      fields.map((field) => $grid.showColumn($grid.getColumnByField(field)));
    } else {
      fields.map((field) => $grid.hideColumn($grid.getColumnByField(field)));
    }
  });
  const dataConditionList = get(memory, "dataCondition", []);
  const dataConditions = form.dataCondition
    ? dataConditionList
        .filter(function ({ lid }) {
          return form.dataCondition.indexOf(lid) > -1;
        })
        .map(function ({ condition }) {
          return condition;
        })
    : [];
  if (dataConditions.length) {
    data["dataCondition"] = dataConditions.join("");
  }
  dataConditionList
    .filter(({ condition }) => condition)
    .map(({ condition, showParam }) => {
      const func =
        dataConditions.indexOf(condition) > -1 ? "showColumn" : "hideColumn";
      $grid[func]($grid.getColumnByField(showParam));
    });
  data["selectType"] = transfer.type == "yszx" ? 2 : 1;
  console.log("custom load: ", conditions, data, form);
  const { data: items, total } = await http.post({
    timeout: 20 * 1000,
    url: "/nyk/search/listMoney",
    data,
  });
  console.log("load: ", data, items, total);
  return { items, total };
};

const c = async function (...args) {
  const [d, l] = args.slice(-2);
  const {
    origin,
    owner: { memory },
  } = d;
  const {
    dc: { close, http },
    xe: { get, set },
  } = l;
  const data = get(origin, "1785949436933836801");
  if (!data) return;
  set(memory, "modal", true);
  console.log("modal button click: ", d, l, data);
  await http.post({ url: "/nyk/program/modifyProgram", data });
  const { guid, tagList = [] } = data;
  await http.post({
    url: "/nyk/tag/insertTableTag",
    data: { guid, idList: tagList, type: "PROGRAM" },
  });
  close();
};

const d = async function (
  event,
  { $grid },
  { params = {}, memory, origin },
  { dc: { open, setModal }, xe: { get } }
) {
  const ret = await new Promise((resolve) => {
    const transfer = params;
    const layout = { id: "1785946370247495682" };
    const modal = {
      title: "添加项目",
      width: "55vw",
      height: document.body.clientHeight * 0.55,
      defaultFullscreen: false,
      destroyOnClose: false,
      maskClosable: false,
      open: true,
      showOkBtn: false,
      showCancelBtn: true,
      afterClose: function () {
        const ret = get(memory, "modal", false);
        delete memory.modal;
        resolve(ret);
      },
    };
    const inner = {};
    console.log("inner click: ", $grid, origin, memory, params, {
      inner,
      layout,
      modal,
      transfer,
    });
    open({ inner, layout, modal, transfer });
  });
  console.log("modal ret: ", ret);
  if (!ret) return;
  $grid.commitProxy("query");
};


const e = async function (node, ...args) {
  const [
    {
      memory,
      transfer: { showContextMenu = false },
    },
    {
      $tree,
      dc: { http, open, reload },
      helper: { treeToList },
      xe: { clone, get },
    },
  ] = args.slice(-2);
  if (!showContextMenu) return [];
  return [
    {
      icon: "bi:plus-lg",
      label: "同步一体化企业",
      handler: async function () {
        const layout = { id: "1797803358876741634" };
        const modal = {
          title: "同步一体化企业",
          width: "55vw",
          minHeight: document.body.clientHeight * 0.75,
          defaultFullscreen: false,
          destroyOnClose: false,
          maskClosable: false,
          open: true,
          showOkBtn: false,
          showCancelBtn: true,
          afterClose: function () {
            reload();
          },
        };
        const inner = {};
        open({ inner, layout, modal, transfer: {} });
      },
    },
    {
      icon: "bi:trash3",
      label: "删除",
      handler: async function () {
        console.log("删除: ", $tree, node);
        const guids = treeToList([node.dataRef])
          .map(function ({ guid }) {
            return guid;
          })
          .join(",");
        console.log("guids: ", guids);
        await http.form({
          url: "/qyk/enterprise/deleteByGuid",
          data: { guids },
        });
        $tree.deleteNodeByKey(node.key);
      },
    },
    {
      icon: "material-symbols:sync",
      label: "同步所有一体化企业",
      handler: async function () {
        await http.form({ url: "/qyk/enterprise/synAll" });
        reload();
      },
    },
  ];
};